<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
/**
 * 记录操作日志
 * @param $module_id        模块ID
 * @param $module_title     模块名称
 * @param $content          操作内容
 * @param $user_id          操作用户ID
 * @param $username         操作用户姓名
 */
function logs($module_id, $module_title, $content, $user_id, $username)
{
    //  验证模块ID
    if ($module_id > 0) {
        //  验证模块名称
        if (empty($module_title)) {
            //  查询模块信息
            $info = db("module")->where("id", "=", $module_id)->find();
            //  定义模块名称
            $module_title = $info["title"];
        }
    }
    //  定义操作内容
    $data = [
        "module_id" => $module_id,
        "module_title" => $module_title,
        "content" => $content,
        "user_id" => $user_id,
        "username" => $username,
        "create_time" => date("Y-m-d H:i:s")
    ];
    //  保存操作数据
    db("logs")->insert($data);
}

/**
 * 上下页
 * @param $where            基础条件
 * @param $order            内容排序号
 * @param $create_time      创建时间
 * @param $table            数据表
 * @return array
 */
function updownPage($id, $where, $order, $table)
{
    $fields = "id,title";
    $uinfo = ["id" => "", "title" => ""];
    $dinfo = ["id" => "", "title" => ""];
    $info = db($table)->where($where)->field($fields)->order($order)->select();
    $nums = 0;
    for ($i = 0; $i < count($info); $i++) {
        if ($info[$i]["id"] == $id) {
            $nums = $i;
            break;
        }
    }
    if ($nums > 0) {
        $uinfo = $info[$nums - 1];
    }
    if ($nums < count($info) - 1) {
        $dinfo = $info[$nums + 1];
    }
    //  定义返回数据
    $arr = array("upinfo" => $uinfo, "downinfo" => $dinfo);
    //  返回数据
    return $arr;
}

/*
 * 处理图片
 */
function processImage($field, $images)
{
    for ($i = 0; $i < count($images); $i++) {
        if (!empty($images[$i][$field])) {
            $images[$i][$field] = config("app.web_path") . str_replace('\\', '/', $images[$i][$field]);
        } else {
            $images[$i][$field] = config("app.web_path") . "/public/static/Admin/img/default.png";
        }
    }
    return $images;
}

/**
 * 处理图片路径
 */
function processImagePath($images)
{
    $images = str_replace('\\', '/', $images);
    return $images;
}

/**
 * 判断当前访问的用户是 PC端 还是 手机端 返回true 为手机端 false 为PC 端
 * @return boolean
 */
function isMobile()
{
    $useragent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
    //echo $useragent;die;
    $useragent_commentsblock = preg_match('|(.*?)|', $useragent, $matches) > 0 ? $matches[0] : '';
    function CheckSubstrs($substrs, $text)
    {
        foreach ($substrs as $substr)
            if (false !== strpos($text, $substr)) {
                return true;
            }
        return false;
    }
    $mobile_os_list = array('Google Wireless Transcoder', 'Windows CE', 'WindowsCE', 'Symbian', 'Android', 'armv6l', 'armv5', 'Mobile', 'CentOS', 'mowser', 'AvantGo', 'Opera Mobi', 'J2ME/MIDP', 'Smartphone', 'Go.Web', 'Palm', 'iPAQ');
    $mobile_token_list = array('Profile/MIDP', 'Configuration/CLDC-', '160×160', '176×220', '240×240', '240×320', '320×240', 'UP.Browser', 'UP.Link', 'SymbianOS', 'PalmOS', 'PocketPC', 'SonyEricsson', 'Nokia', 'BlackBerry', 'Vodafone', 'BenQ', 'Novarra-Vision', 'Iris', 'NetFront', 'HTC_', 'Xda_', 'SAMSUNG-SGH', 'Wapaka', 'DoCoMo', 'iPhone', 'iPod');

    $found_mobile = CheckSubstrs($mobile_os_list, $useragent_commentsblock) ||
        CheckSubstrs($mobile_token_list, $useragent);

    if ($found_mobile) {
        return true;
    } else {
        return false;
    }
}

/*
 * 返回上午下午
 */
function getStrTime()
{
    $no = date("H", time());
    if ($no > 0 && $no <= 6) {
        return "凌晨好";
    }
    if ($no > 6 && $no < 12) {
        return "上午好";
    }
    if ($no >= 12 && $no <= 18) {
        return "下午好";
    }
    if ($no > 18 && $no <= 24) {
        return "晚上好";
    }
    return "您好";
}

/**
 * 获取随机数
 */
function smsCode()
{
    $number = rand(0, 9);
    $number .= rand(0, 9);
    $number .= rand(0, 9);
    $number .= rand(0, 9);
    return $number;
}

/**
 * 获取真实IP
 */
function get_real_ip(){ 
    $ip=false; 
    if(!empty($_SERVER['HTTP_CLIENT_IP'])){ 
        $ip=$_SERVER['HTTP_CLIENT_IP']; 
    }
    if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){ 
        $ips=explode (', ', $_SERVER['HTTP_X_FORWARDED_FOR']); 
        if($ip){ array_unshift($ips, $ip); $ip=FALSE; }
        for ($i=0; $i < count($ips); $i++){
            if(!eregi ('^(10│172.16│192.168).', $ips[$i])){
                $ip=$ips[$i];
                break;
            }
        }
    }
    return ($ip ? $ip : $_SERVER['REMOTE_ADDR']); 
}

/**
 * 获取订单号
 */
function getOrderNo()
{
    $number = date('YmdHis') . str_pad(mt_rand(1, 999), 3, '0', STR_PAD_LEFT);
    return $number;
}